package com.dcrzl.gazgmh.module.mh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.dcrzl.gazgmh.common.entity.mh.MhIndexModule;
import com.dcrzl.gazgmh.common.entity.mh.MhModule;
import com.dcrzl.gazgmh.common.mapper.mh.MhIndexModuleMapper;
import com.dcrzl.gazgmh.common.service.impl.BaseServiceImpl;
import com.dcrzl.gazgmh.common.utils.PageInfo;
import com.dcrzl.gazgmh.common.utils.Result;
import com.dcrzl.gazgmh.common.utils.SysConstant;
import com.dcrzl.gazgmh.module.mh.service.MhIndexModuleService;
import com.dcrzl.gazgmh.module.mh.service.MhModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.*;

@Service("mhIndexModuleService")
public class MhIndexModuleServiceImpl extends BaseServiceImpl<MhIndexModuleMapper, MhIndexModule> implements MhIndexModuleService {

    @Autowired
    private MhModuleService mhModuleService;

    private LambdaQueryWrapper<MhIndexModule> commonWrapper(MhIndexModule mhIndexModule) {
        LambdaQueryWrapper<MhIndexModule> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Objects.nonNull(mhIndexModule.getMhModule()), MhIndexModule::getModuleId, mhIndexModule.getModuleId());
        wrapper.eq(Objects.nonNull(mhIndexModule.getActiveStatus()), MhIndexModule::getActiveStatus, mhIndexModule.getActiveStatus());
        wrapper.eq(Objects.nonNull(mhIndexModule.getStatus()), MhIndexModule::getStatus, mhIndexModule.getStatus());
        wrapper.eq(Objects.nonNull(mhIndexModule.getType()), MhIndexModule::getType, mhIndexModule.getType());
        return wrapper;
    }

    @Override
    public Result<MhIndexModule> selectOne(MhIndexModule mhIndexModule) {
        LambdaQueryWrapper<MhIndexModule> wrapper = this.commonWrapper(mhIndexModule);
        return super.selectOne(wrapper);
    }

    @Override
    public Result<List<MhIndexModule>> selectList(MhIndexModule mhIndexModule) {
        LambdaQueryWrapper<MhIndexModule> wrapper = this.commonWrapper(mhIndexModule);
        wrapper.orderByAsc(MhIndexModule::getOrderNum).orderByDesc(MhIndexModule::getId);
        return super.selectList(wrapper);
    }

    @Override
    public PageInfo<MhIndexModule> selectPage(MhIndexModule mhIndexModule) {
        LambdaQueryWrapper<MhIndexModule> wrapper = this.commonWrapper(mhIndexModule);
        return super.selectPage(mhIndexModule, wrapper);
    }

    @Override
    public PageInfo<MhIndexModule> selectUPage(MhIndexModule mhIndexModule) {
        PageInfo<MhIndexModule> mhIndexModulePageInfo = this.selectPage(mhIndexModule);
        List<MhIndexModule> list = mhIndexModulePageInfo.getList();
        Set<Long> moduleIdSet = new HashSet<>();
        list.forEach(mhIndexModule1 -> {
            moduleIdSet.add(mhIndexModule1.getModuleId());
        });
        if (!CollectionUtils.isEmpty(moduleIdSet)) {
            Result<List<MhModule>> listResult = mhModuleService.selectList(new MhModule().setIdList(moduleIdSet));
            if (listResult.isSuccess()) {
                List<MhModule> data = listResult.getData();
                Map<Long, MhModule> map = new HashMap<>();
                data.forEach(mhModule -> {
                    map.put(mhModule.getId(), mhModule);
                });
                list.forEach(mhIndexModule1 -> {
                    mhIndexModule1.setMhModule(map.get(mhIndexModule1.getModuleId()));
                });
            }
        }
        return mhIndexModulePageInfo;
    }

    @Override
    public Result<List<MhIndexModule>> selectUList(MhIndexModule mhIndexModule) {
        Result<List<MhIndexModule>> listResult = this.selectList(mhIndexModule);
        if (listResult.isSuccess()) {
            Set<Long> moduleIdSet = new HashSet<>();
            List<MhIndexModule> list = listResult.getData();
            list.forEach(mhIndexModule1 -> {
                moduleIdSet.add(mhIndexModule1.getModuleId());
            });
            if (!CollectionUtils.isEmpty(moduleIdSet)) {
                Result<List<MhModule>> listResult1 = mhModuleService.selectUList(new MhModule().setIdList(moduleIdSet));
                if (listResult1.isSuccess()) {
                    List<MhModule> data = listResult1.getData();
                    Map<Long, MhModule> map = new HashMap<>();
                    data.forEach(mhModule -> {
                        map.put(mhModule.getId(), mhModule);
                    });
                    list.forEach(mhIndexModule1 -> {
                        mhIndexModule1.setMhModule(map.get(mhIndexModule1.getModuleId()));
                    });
                }
            }
        }
        return listResult;
    }

    @Override
    public Result<Integer> removeById(Serializable id) {
        MhIndexModule mhIndexModule = new MhIndexModule().setStatus(SysConstant.DATA_DELETE_STATUS)
                .setId((Long) id);
        return this.updateById(mhIndexModule);
    }

}